package com.example.administrator.base.adapt;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.administrator.base.activity.AddDispatchListActivity;
import com.example.administrator.base.activity.test;
import com.example.administrator.base.bean.ModelMainItem;
import com.example.administrator.base.R;
import java.util.List;
import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.pedant.SweetAlert.SweetAlertDialog;


public class AdapterRecyclerViewMain extends RecyclerView.Adapter<AdapterRecyclerViewMain.ViewHolder> {
    private Context context;
    private List<ModelMainItem> mValues;
    public AdapterRecyclerViewMain(List<ModelMainItem> items) {
        mValues = items;
    }

    @Override
    public AdapterRecyclerViewMain.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview_main, parent, false);
        context = view.getContext();

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        holder.mItem = mValues.get(position);
        holder.tvName.setText(holder.mItem.getName());
        holder.imageView.setBackgroundResource(holder.mItem.getImage());
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String type = holder.mItem.getType();
                if (type.equals("adddispatchlist")) {
                    SweetAlertDialog sweetAlertDialognew = new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE);
                    sweetAlertDialognew.setCanceledOnTouchOutside(true);
                    sweetAlertDialognew
                            .setTitleText("发起新的工单")
                            .setContentText("如果您要填写调度单，请选择“是”")
                            .setConfirmText("是")
                            .setCancelText("否")
                            .showCancelButton(true)
                            .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sDialog) {
                                    Intent intent = new Intent(context, AddDispatchListActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                    intent.putExtra("type", type);
                                    context.startActivity(intent);
                                    sDialog.dismiss();

                                }
                            })
                            .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    Intent intent = new Intent(context, test.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                                    intent.putExtra("type", type);
                                    context.startActivity(intent);
                                    sweetAlertDialog.dismiss();
                                }
                            })

                            .show();
                } else {

                    skipActivity(context, holder.mItem.getActivity(), holder.mItem.getType());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.imageView)
        ImageView imageView;
        @InjectView(R.id.tv_name)
        TextView tvName;

        ModelMainItem mItem;
        final View mView;

        ViewHolder(View view) {
            super(view);
            mView = view;
            ButterKnife.inject(this, view);
        }
    }

    private void skipActivity(Context context, Class<?> goal, String type) {
        Intent intent = new Intent(context, goal);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }
}
